package com.zp.security.jwt.handler;

import com.zp.common.base.constant.ResultCode;
import com.zp.common.base.resp.BaseResp;
import com.zp.common.base.utils.ResponseUtils;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 认证失败通过AuthenticationFailureHandler进行过处理
 * 因此AuthenticationEntryPoint我们用作用户访问受保护的资源，但是用户没有通过认证则会进入这个处理器
 *
 * @author zp
 */
@Component
public class EntryPointUnauthorizedHandler implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
        ResponseUtils.result(response,new BaseResp(ResultCode.NO_LOGIN));
    }
}
